package com.tedu.cloud_vehicle_plus.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tedu.cloud_vehicle_plus.base.response.JsonResult;
import com.tedu.cloud_vehicle_plus.base.response.StatusCode;
import com.tedu.cloud_vehicle_plus.mapper.VehicleMapper;
import com.tedu.cloud_vehicle_plus.pojo.dto.VehicleAddParam;
import com.tedu.cloud_vehicle_plus.pojo.dto.VehicleListQuery;
import com.tedu.cloud_vehicle_plus.pojo.entity.User;
import com.tedu.cloud_vehicle_plus.pojo.entity.Vehicle;
import com.tedu.cloud_vehicle_plus.pojo.vo.UserVO;
import com.tedu.cloud_vehicle_plus.pojo.vo.VehicleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "02.车辆管理模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/v1/vehicle/")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @PostMapping("add")
    @ApiOperation(value = "添加车辆")
    @ApiOperationSupport(order = 10)
    public JsonResult add(@RequestBody VehicleAddParam vehicleAddParam, @ApiIgnore HttpSession session) {
        log.debug("调试----vehicleAddParam: " + vehicleAddParam);

        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        UserVO userVO = (UserVO) session.getAttribute("user");
        vehicle.setUserId(userVO.getId());
        vehicle.setIsDelete(0);
        log.debug("调试----vehicle: " + vehicle);

        int count = vehicleMapper.insertVehicle(vehicle);
        return count > 0 ? JsonResult.ok() : new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @GetMapping("list")
    @ApiOperation(value = "车辆列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list(VehicleListQuery vehicleListQuery) {
        log.debug("调试----vehicleListQuery: " + vehicleListQuery);
        List<VehicleVO> vehicleVOList = vehicleMapper.vehicleList(vehicleListQuery);
        return vehicleVOList != null ? JsonResult.ok(vehicleVOList) : new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @PostMapping("del/{id}")
    @ApiOperation(value = "删除车辆")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParam(name = "id", value = "车辆编号id", required = true)
    public JsonResult del(@Range(min = 201, max = 1000, message = "车辆的编号必须在201-1000之间") @PathVariable Long id, @ApiIgnore HttpSession session) {
        log.debug("调试----车辆编号id: " + id);

        UserVO userVO = (UserVO) session.getAttribute("user");
        Long userId = userVO.getId();
        int count = vehicleMapper.deleteVehicleById(id, userId);
        return count > 0 ? JsonResult.ok() : new JsonResult(StatusCode.OPERATION_FAILED);
    }
}
